package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.repairOrder_report.RepairOrderReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IRepairOrderReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;

@Slf4j
@Service
public class RepairOrderReportServiceImpl implements IRepairOrderReportService {



    public List<RepairOrderReportGridOut> getGridData(ConditionBean conditions) {
        //查询实时维修单表: ret_repair_order
        final SqlVo sqlVo1 = new SqlVo();
        sqlVo1.appendSql("SELECT");
        sqlVo1.appendSql("b_1.data_desc AS FAB_ID,");
        sqlVo1.appendSql("b_2.data_desc AS LINE_ID,");
        sqlVo1.appendSql("b_3.tool_dsc AS TOOL_ID,");
        sqlVo1.appendSql("G.usr_name AS eqt_confirmer_name,");
        sqlVo1.appendSql("H.usr_name AS last_deal_name,");
        sqlVo1.appendSql("A.*");
        sqlVo1.appendSql("FROM icim_jn.ret_repair_order A");
        sqlVo1.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'FBID') b_1 ON A.FAB_ID_FK = b_1.data_ext");
        sqlVo1.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'AREA') b_2 ON A.LINE_ID_FK = b_2.data_ext");
        sqlVo1.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_3 ON A.TOOL_ID_FK = b_3.tool_id");
        sqlVo1.appendSql("LEFT JOIN bis_user G ON A.eqt_confirmer = G.usr_id");
        sqlVo1.appendSql("LEFT JOIN bis_user H ON A.last_deal = H.usr_id");
        sqlVo1.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo1.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo1.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo1.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in " + jointInSql(conditions.getToolg_id()) + "))");
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo1.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo1.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getRepair_order_id())) sqlVo1.appendSql("AND A.REPAIR_ORDER_ID in" + jointInSql(conditions.getRepair_order_id()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo1.appendSqlAndAddParam("AND A.BEGIN_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo1.appendSqlAndAddParam("AND A.BEGIN_TIMESTAMP <= ?", conditions.getEnd_time());
        sqlVo1.appendSql("ORDER BY A.BEGIN_TIMESTAMP");

        //查询历史维修单表: ret_repair_order_history
        final SqlVo sqlVo2 = new SqlVo();
        sqlVo2.appendSql("SELECT");
        sqlVo2.appendSql("b_1.data_desc AS FAB_ID,");
        sqlVo2.appendSql("b_2.data_desc AS LINE_ID,");
        sqlVo2.appendSql("b_3.tool_dsc AS TOOL_ID,");
        sqlVo2.appendSql("G.usr_name AS eqt_confirmer_name,");
        sqlVo2.appendSql("H.usr_name AS last_deal_name,");
        sqlVo2.appendSql("B.*");
        sqlVo2.appendSql("FROM icim_jn.ret_repair_order_history B");
        sqlVo2.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'FBID') b_1 ON B.FAB_ID_FK = b_1.data_ext");
        sqlVo2.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'AREA') b_2 ON B.LINE_ID_FK = b_2.data_ext");
        sqlVo2.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_3 ON B.TOOL_ID_FK = b_3.tool_id");
        sqlVo2.appendSql("LEFT JOIN bis_user G ON B.eqt_confirmer = G.usr_id");
        sqlVo2.appendSql("LEFT JOIN bis_user H ON B.last_deal = H.usr_id");
        sqlVo2.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo2.appendSql("AND B.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo2.appendSql("AND B.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo2.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in " + jointInSql(conditions.getToolg_id()) + "))");
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo2.appendSql("AND B.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo2.appendSql("AND B.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getRepair_order_id())) sqlVo2.appendSql("AND A.REPAIR_ORDER_ID in" + jointInSql(conditions.getRepair_order_id()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo2.appendSqlAndAddParam("AND B.BEGIN_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo2.appendSqlAndAddParam("AND B.BEGIN_TIMESTAMP <= ?", conditions.getEnd_time());
        sqlVo2.appendSql("ORDER BY B.BEGIN_TIMESTAMP");


        List<RepairOrderReportGridOut> resultDataList = new ArrayList<>();
        List<RepairOrderReportGridOut> resultDataList_1 = mySql_QueryRunner_QueryListBySql(sqlVo1, RepairOrderReportGridOut.class);
        List<RepairOrderReportGridOut> resultDataList_2 = mySql_QueryRunner_QueryListBySql(sqlVo2, RepairOrderReportGridOut.class);
        resultDataList.addAll(resultDataList_1);
        resultDataList.addAll(resultDataList_2);

        resultDataList.forEach(data -> {
            String repair_stat = "";
            switch (data.getRepair_stat() == null ? "" : data.getRepair_stat()) {
                case "CLOS": repair_stat = "已结单"; break;
                case "COMP": repair_stat = "已完成"; break;
                case "INPR": repair_stat = "进行中"; break;
                case "CHAN": repair_stat = "已转单"; break;
            }
            data.setRepair_stat(repair_stat);

            String come_from = "";
            switch (data.getCome_from() == null ? "" : data.getCome_from()) {
                case "A": come_from = "警报"; break;
                case "N": come_from = "新建"; break;
                case "O": come_from = "转单"; break;
            }
            data.setCome_from(come_from);
        });

        return resultDataList.stream().sorted(Comparator.comparing(RepairOrderReportGridOut::getBegin_timestamp)).collect(Collectors.toList());
    }



}
